package com.hxzy.tms.service.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import com.hxzy.tms.HibernateSessionHolder;
import com.hxzy.tms.SessionHolder;
import com.hxzy.tms.dao.UserDao;
import com.hxzy.tms.entity.User;
import com.hxzy.tms.service.ServiceResult;
import com.hxzy.tms.service.UserService;
/**
 * 用户Service实现类
 * @author team1
 *
 */
public class DefaultUserService implements UserService {
	private UserDao userDao;
	public boolean checkEmail(String email) {
		// TODO Auto-generated method stub
		return false;
	}

	public User login(String username, String password, String remberMe, String ip) {
		// TODO Auto-generated method stub
		return null;
	}

	public ServiceResult<User> register(String email, String passwd, String nickname, String realname, long mobile,
			String vercode, String ip) {
		// TODO Auto-generated method stub
		return null;
	}

	public User getInfo(String email) {
		// TODO Auto-generated method stub
		return null;
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public ServiceResult<String> generateSessionHash(String ip) {
        long sessionUser = SessionHolder.get().getId();
        int s = RandomUtils.nextInt(10000, 99999); // 随机数

        String hash = DigestUtils.sha256Hex((s + ip).getBytes());
        User user = userDao.read(User.class, sessionUser);
         user.setHash(hash);
        long hashExpires = (System.currentTimeMillis() / 1000) + 7 * 24 * 60 * 60;
        user.setGmtHashExpires(hashExpires); // 超时时间是 7 天
        if (userDao.update(user) == 1) {
            return new ServiceResult<String>(1, hash);
        } else {
            return new ServiceResult<String>(-2, null, "数据库错误");
        }
	}

	public ServiceResult<User> getUser(String hash, String ip) {
		 if (StringUtils.isBlank(hash) || StringUtils.isBlank(ip)) {
	            return new ServiceResult<User>(1, null);
	        }
	        if (HibernateSessionHolder.get() != null) {
	            return new ServiceResult<User>(1, SessionHolder.get());
	        }

	        if (StringUtils.isEmpty(ip) || StringUtils.isEmpty(hash)) {
	            return new ServiceResult<User>(1, null);
	        }

	        Map<String, Object> param = new HashMap<String, Object>();
	        param.put("hash", hash);
	        param.put("gmt_hash_expires", (System.currentTimeMillis() / 1000));
	        List<User> users = userDao.read("from User where hash = :hash and gmthashexpires > :gmt_hash_expires ", param);
	        if (users.isEmpty()) {
	            return new ServiceResult<User>(1, null);
	        }

	        User user = users.get(0);

	        if (!ip.equals(user.getIpLogin())) { // 如果换 ip 了就当没有登录过啦，TODO 要清空 hash
	            return new ServiceResult<User>(1, null);
	        }

	        User userInSession = new User();
	        userInSession.setId(user.getId());
	        userInSession.setNickname(user.getNickname());
	        return new ServiceResult<User>(1, userInSession);
	}
    
}